<!--管理员查看通知列表-->
<link rel="stylesheet" href="./css/messageamend.css">

<div class="modal-dialog modal fade" id="fabu1">
	<div class="modal-content">
		<div class="modal-header">
			<button type="button" class="close" data-dismiss="modal">&times;</button>
		</div>
		<div class="modal-body">
			<p>发布成功!点击确认返回到通知页</p>
		</div>
		<div class="modal-footer">
			<button id="ture" type="button" class="btn btn-primary" class="close" data-dismiss="modal">确定</button>
		</div>
	</div>
</div>

<div id="big">

</div>

<nav aria-label="...">
	<ul class="pager">
		<li>
			<a href="#" id="pre">上一页</a>
		</li>
		<li>
			<a href="#" id="top">首页</a>
		</li>
		<li>第<span class="num">1</span>页</li>
		<li>
			<a href="#" id="end">尾页</a>
		</li>
		<li>
			<a href="#" id="next">下一页</a>
		</li>
		<a href="#" id="fabu_anniu"><button class="btn btn-primary ">添加通知</button></a>
	</ul>
</nav>

<script>
	$('#fabu_anniu').click(function() {
		$('.center').html('');
		$('.center').load('./announce.html');
	})

	var pageSize = 5;

	function fenye(pageNum) {
		$.ajax({
			xhrFields: {
				withCredentials: true
			},
			type: "GET",
			url: getFullURL("inform-list"),
			data: {
				pageNum: pageNum,
				pageSize: pageSize
			},
			success: function(data) {
				if(data.status == 0) {
					$.session.set("prePageNum", data.page.prePageNum);
					$.session.set("nextPageNum", data.page.nextPageNum);
					$.session.set("totalPageNum", data.page.totalPageNum);
					$('.num').html(data.page.pageNum);

					$('#big').html("");

					var list = data.page.list;
					for(var i = 0; i < list.length; i++) {
						var str = `
				          <div class="tongzhi" id="look1_` + list[i].iid + `">
				            <div class="btn3">
				            <button class="xiugai btn btn-primary" id="xiugai_` + list[i].iid + `" value="` + list[i].iid + `">修改</button>
				            <button class="remove btn btn-primary" id="remove_` + list[i].iid + `" value="` + list[i].iid + `">删除</button>
				            </div>
				            <input type="hidden" id="hidden" value="` + list[i].iid + `"/>
				            <div class="title_div">` + list[i].title + `</div>
				            <div class="content">` + list[i].content + `</div>
				            <div class="footer">` + timeStamp2String(list[i].createTime) + `</div>
				            <span class="id">` + list[i].iid + `</span>
				          </div>
				          `;

						var div = $('<div></div>');
						div.html(str);
						$('#big').append(div);

						var look = '#look1_' + list[i].iid;
						var xiugai = '#xiugai_' + list[i].iid;

						$(look).click(function() {
							$.session.set('iid', $(this).children("input").eq(0).val());
							$('.center').html('');
							$('.center').load('./amend.html');
						})

						$(xiugai).click(function() {
							$.session.set('iid', $(this).val());
							$('.center').html('');
							$('.center').load('./amend.html');
							return false;
						})

						var str1 = `
				          <div class="modal-dialog modal fade kk" id="anniu1_` + list[i].iid + `" >
				            <div class="modal-content">
				              <div class="modal-header">
				                <button type="button" class="close" data-dismiss="modal">&times;</button>
				                <h4>提示</h4>
				              </div>
				              <div class="modal-body">
				                <p>是否确认删除?</p>
				              </div>
				              <div class="modal-footer">
				                <button type="button" class="btn btn-default" class="close" data-dismiss="modal">取消</button>
				                <button type="button" class="btn btn-primary" class="close" id="ok_` + list[i].iid + `" data-dismiss="modal" value="` + list[i].iid + `">确认</button>
				              </div>
				            </div>
				          </div>
				          `;

						var div1 = $('<div></div>');
						div1.html(str1);
						$('#big').append(div1);

						var remove = '#remove_' + list[i].iid;

						$(remove).click(function() {
							var anniu1 = '#anniu1_' + $(this).val();
							$(anniu1).modal({
								keyboard: true,
								show: true
							})
							return false;
						})

						var ok = '#ok_' + list[i].iid;
						$(ok).click(function() {
							$.ajax({
								xhrFields: {
									withCredentials: true
								},
								type: 'GET',
								url: getFullURL("admin/del-inform"),
								data: {
									iid: $(this).val()
								},
								dataType: 'json',
								success: function(data) {
									if(data.status == 0) {
										setTimeout(() => {
											$('.center').html('');
											$('.center').load('./messageamend.html');
										}, 300);
									}
								},
								error: function(err) {
									alert("请求异常，请重新登录！", function() {
										$(location).attr('href', './login.html');
									})
								}
							})
						})
						//for结尾
					}
				}
				console.log(data);
			},
			error: function(err) {
				alert("请求异常，请重新登录！", function() {
					$(location).attr('href', './login.html');
				})
			}
		})
	}

	fenye(1)

	$("#top").click(function() {
		fenye(1);
	})
	$("#end").click(function() {
		fenye($.session.get("totalPageNum"));
	})
	$("#pre").click(function() {
		fenye($.session.get("prePageNum"));
	})
	$("#next").click(function() {
		fenye($.session.get("nextPageNum"));
	})

	function timeStamp2String(time) {
		var datetime = new Date();
		datetime.setTime(time);
		var year = datetime.getFullYear();
		var month = datetime.getMonth() + 1 < 10 ? "0" + (datetime.getMonth() + 1) : datetime.getMonth() + 1;
		var date = datetime.getDate() < 10 ? "0" + datetime.getDate() : datetime.getDate();
		var hour = datetime.getHours() < 10 ? "0" + datetime.getHours() : datetime.getHours();
		var minute = datetime.getMinutes() < 10 ? "0" + datetime.getMinutes() : datetime.getMinutes();
		var second = datetime.getSeconds() < 10 ? "0" + datetime.getSeconds() : datetime.getSeconds();
		return year + "-" + month + "-" + date + " " + hour + ":" + minute + ":" + second;
	}
</script>